<?PHP
	
	include("../includes/configPages.php");
	include("../includes/forum/category.class.php");
	include("../includes/forum/board.class.php");
	include("../includes/forum/topic.class.php");
	include("../includes/forum/post.class.php");
	$category = new category();
	$board = new board();
	$topic = new topic();
	$post = new post();

	/* registration variables */
	$variableStack['forumTitle'] = $config->getVal($db, 'board_name');
	$variableStack['style_name'] = $activeStyle;
	
	$html = file_get_contents('../styles/'.$activeStyle.'/t/forum.html');
	$html = parseHtml($html, $variableStack);
	echo $html;
	
	$categories = $category->getAllCategorys($db);
	
	while($row=mysql_fetch_object($categories)){
		$variableStack['forum_boards'] = "";
		$html = file_get_contents('../styles/'.$activeStyle.'/t/forum_cat.html');
		$outHtml = "";
		
		$boards = $board->getAllBoards($db, $row->cat_id);
		while($boardRow=mysql_fetch_object($boards)){
			$boardHtml = file_get_contents('../styles/'.$activeStyle.'/t/forum_cat_board.html');
			$variableStack['board_name'] = $boardRow->board_name;
			$variableStack['board_desc'] = $boardRow->board_desc;
			$variableStack['board_id'] = $boardRow->board_id;
			
			$allTopics = $topic->getAllTopics($db, $boardRow->board_id, NULL, 1);
			$variableStack['board_topics'] = mysql_num_rows($allTopics);
			$allPosts = $db->query("SELECT * FROM ".$prefix."forum_post a INNER JOIN ".$prefix."forum_topic b ON a.topic_id=b.topic_id WHERE b.board_id=".$boardRow->board_id." AND a.post_deleted=0 AND b.deleted=0");
			$variableStack['board_posts'] = mysql_num_rows($allPosts);
			$lastPost = $board->getLastPost($db, $boardRow->board_id);
			
			$lastPost = $board->getLastPost($db, $boardRow->board_id);
			if(mysql_num_rows($lastPost)==0){
				$variableStack['board_last_post'] = $lang['time_never'];
			} else {
				while($postRow = mysql_fetch_object($lastPost)){
					$lastPostTime = $postRow->post_time;
					$nowTime = time();
					$difference = $nowTime - $lastPostTime;
					if($difference<86400){ //1 day
						if(	$difference/3600 < 1 ){
							if($difference < 60){
								
								$variableStack['board_last_post'] = $lang['time_less_minute'];
							} else {
								$variableStack['board_last_post'] = sprintf($lang['time_less_hour'],round($difference/60));
							}
						} else {
						   	$variableStack['board_last_post'] = sprintf($lang['time_over_hour'], round($difference/3600));
						}
					} else {
						$variableStack['board_last_post'] = sprintf($lang['time_over_day'], round($difference/86400));
					}
				}
			}
			$outHtml .= parseHtml($boardHtml, $variableStack);	
		}
		$variableStack['forum_boards'] = $outHtml;
		
		$variableStack['cat_name'] = $row->cat_name;
		$variableStack['cat_desc'] = $row->cat_desc;
		
		$html = parseHtml($html, $variableStack);
		echo $html;
	}
	
	
	
?>
